const gulp = require("gulp");
const uglify = require('gulp-uglify');
const pump = require('pump');
const cleanCSS = require('gulp-clean-css');
/***************** *****************************
    相关文档：
    https://www.npmjs.com/package/gulp-html-tpl
***********************************************/
const htmltpl = require('gulp-html-tpl');
const artTemplate = require('art-template');

// 使用template，共同的模版例如头部，侧边等，编译到每一个pagesTemp中
gulp.task('commonHtml', function () {
    return gulp.src('./src/pagesTemp/*.html')
        .pipe(htmltpl({
            tag: 'template',
            paths: ['./src/commonTemp'],
            engine: function (template, data) {
                return artTemplate.compile(template)(data)
            },
            data: {
                useHeader: false
            },
            beautify: {
                indent_char: ' ',
                indent_with_tabs: false
            }
        }))
        .pipe(gulp.dest('./dist/'));
});

gulp.task('moveimg', function () {
    return gulp.src(
        ['./src/imgs/**']
    ).pipe(gulp.dest('./dist/imgs'));
});

gulp.task('movejs', function (cb) {
    pump([
        gulp.src('./src/js/**'),
        uglify(),
        gulp.dest('./dist/js')
    ],
        cb
    );
});

gulp.task('movecss', () => {
    return gulp.src(['./src/css/**'])
        .pipe(cleanCSS())
        .pipe(gulp.dest('./dist/css'));
});

gulp.task('moveres', function () {
    return gulp.src(
        ['./src/res/**']
    ).pipe(gulp.dest('./dist/res/'));
});


gulp.task('watch', function () {
    var watcher = gulp.watch(['./src/**'], ['commonHtml', 'moveimg', 'moveres', 'movecss', 'movejs']);
    watcher.on('change', function (event) {
        console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
    });
});

gulp.task('default', ['watch', 'commonHtml', 'moveimg', 'moveres', 'movecss', 'movejs']);
